package glazedlists.examples;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.TextFilterator;
import ca.odell.glazedlists.matchers.TextMatcherEditor;
import ca.odell.glazedlists.swing.AutoCompleteSupport;

import javax.swing.*;
import java.awt.*;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.util.Arrays;
import java.util.List;

public class AutoCompleteSupportExample2 {

    public static final class Url {
        private final String location;

        public Url(String location) {
            this.location = location;
        }

        public String getLocation() {
            return location;
        }

        public static Url valueOf(String s) {
            return new Url(s);
        }

        @Override
        public String toString() {
            return location;
        }
    }

    private static final Url[] URLS = {
            new Url("http://mail.google.com/mail/"),
            new Url("http://slashdot.org/"),
            new Url("http://www.clientjava.com/blog"),
            new Url("del.icio.us"),
            new Url("http://java.sun.com/javase/6/"),
            new Url("http://java.sun.com/"),
            new Url("http://java.sun.com/j2se/1.5.0/download.jsp"),
            new Url("http://java.sun.com/javaone/sf/"),
            new Url("http://www.jetbrains.com/"),
            new Url("http://www.jetbrains.com/idea/?ggl502"),
            new Url("http://www.wilshipley.com/blog/"),
            new Url("http://jroller.com/page/fate"),
            new Url("http://wilwheaton.typepad.com/"),
            new Url("http://www.theonion.com/content/"),
            new Url("http://www.indeed.com/")
    };

    public static void main(String[] args) throws Exception {
        final JComboBox comboBox = new JComboBox();
        comboBox.setPrototypeDisplayValue(new Url("http://java.sun.com/j2se/1.5.0/download.jsp"));

        final EventList<Url> urls = GlazedLists.eventList(Arrays.asList(URLS));

        SwingUtilities.invokeAndWait(() -> {
            AutoCompleteSupport support = AutoCompleteSupport.install(comboBox, urls, new UrlTextFilterator(), new UrlFormat());
            support.setFilterMode(TextMatcherEditor.CONTAINS);
        });

        final JFrame frame = new JFrame("AutoComplete Test");
        frame.getContentPane().setLayout(new GridBagLayout());
        frame.getContentPane().add(comboBox,
                new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                        new Insets(20, 20, 20, 20), 0, 0));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private static final class UrlTextFilterator implements TextFilterator<Url> {
        @Override
        public void getFilterStrings(List<String> list, Url url) {
            final String location = url.getLocation();
            list.add(location);

            if (location.startsWith("http://")) {
                list.add(location.substring(7));
            }
            if (location.startsWith("https://www.")) {
                list.add(location.substring(11));
            }
        }
    }

    private static final class UrlFormat extends Format {
        @Override
        public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
            if (obj != null) {
                toAppendTo.append(((Url) obj).getLocation());
            }
            return toAppendTo;
        }

        @Override
        public Object parseObject(String source, ParsePosition pos) {
            return new Url(source);
        }
    }
}
